<?php


namespace app\admin\controller\order;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;

class Orderafter extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['o.is_del', '=', 0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
            }
            //获取列表
            $_list = Db::name('order_after')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->field('o.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->page($page, $limit)
                ->order('o.create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            //获取数量
            $total = Db::name('order_after')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign('proviec', $proviec);
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $stmt = Db::name('order_after')->where('id', $data['id'])->update($data['row']);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('order_after')->find($id);
        $this->assign('data', $data);
        return $this->fetch('');

    }

    //退款
    public function refund()
    {
        $param = $this->request->param();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['row']['tui_price']<0){
                $this->error('请输入正确数值！');
            }
            if ($data['row']['tui_price']>$data['row']['pay_money']){
                $this->error('退款金额不能大于支付金额');
            }
            $this->tui($data['row']['order_after_id'],$data['row']['order_id'],$data['row']['tui_price']);
            $this->success('操作成功！');
        }
        $data = Db::name('order')->find($param['order_id']);
        $this->assign([
            'data' => $data,
            'order_after_id'=>$param['id']
        ]);
        return $this->fetch('');
    }

    public function goods_list(){
        $id = $this->request->param('id/d', 0);
        $goods_list = Db::name('order_goods')
            ->where(['order_id' => $id])
            ->order('sort desc')
            ->select();
        foreach ($goods_list as $k => $v) {
            $goods_info=Db::name('goods')->find($v['goods_id']);
            $goods_list[$k]['goods_info']=$goods_info;
        }
        $this->assign([
            'goods_list' => $goods_list
        ]);
        return $this->fetch('server');
    }
    public function tuishen(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //2同意 3拒绝
            if ($data['status']==2){
                $update_data=array(
                    'id'=>$data['id'],
                    'status'=>$data['status'],
                    'status_time'=>time(),
                    'people'=>$data['people'],
                    'phone'=>$data['phone'],
                    'address'=>$data['address'],
                );
            }
            if ($data['status']==3){
                $update_data=array(
                    'id'=>$data['id'],
                    'status'=>$data['status'],
                    'status_time'=>time(),
                    'refuse_reason'=>$data['refuse_reason'],
                );
            }
            Db::name('order_after')->update($update_data);
            $this->success('操作成功！');
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('order_after')->find($id);
        $data['img']=explode(',',$data['imgs']);
        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch('');
    }
    public function tui($order_after_id,$order_id, $tui_money = 0)
    {
        $order_info = Db::name('order')
            ->where('id', $order_id)
            ->find();
        $config = config('wechat.wxpay');
        $app = Factory::payment($config);
        if (!$tui_money) {
            $tui_money = $order_info['pay_money'];
        }
        $result = $app->refund->byOutTradeNumber($order_info['order_no'], $order_info['order_no'], $order_info['pay_money'] * 100, $tui_money * 100, [
            // 可在此处传入其他参数，详细参数见微信支付文档
            'refund_desc' => '订单退款',
        ]);
        $order_after_update_data=array(
            'id'=>$order_after_id,
            'status' => 5,
            'tui_money'=>$tui_money,
        );
        Db::name('order_after')->update($order_after_update_data);
        tuikuanServerOrder($order_info, $order_info['pay_money']);
    }
}